<?php

require_once dirname(__FILE__).'/../lib/elemento_carreraGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/elemento_carreraGeneratorHelper.class.php';

/**
 * elemento_carrera actions.
 *
 * @package    sistema_estadisticas
 * @subpackage elemento_carrera
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */

class elemento_carreraActions extends autoElemento_carreraActions
{

	public function execute($request)
	{
		$carrera_slug = $request->getUrlParameter('carrera_slug');

		if (!CarreraUsuarioTable::getInstance()->existsAccess($carrera_slug, $this->getUser()->getUserId()))
			$this->forward('login', 'secure');

		$this->getContext()->getRouting()->setDefaultParameter('carrera_slug', $carrera_slug);

		return parent::execute($request);
	}

	public function executeIndex(sfWebRequest $request)
	{
		parent::executeIndex($request);
		$carrera = CarreraTable::getInstance()->getNameBySlug($request->getParameter('carrera_slug'));
		$this->getUser()->setFlash('info', $carrera, false);
	}

	public function executeNew(sfWebRequest $request)
	{
		$carrera_id = CarreraTable::getInstance()->getIdBySlug($request->getUrlParameter('carrera_slug'));
		$this->elemento_carrera = new ElementoCarrera();
		$this->elemento_carrera->setCarreraId($carrera_id);
		
		$this->form = $this->configuration->getForm($this->elemento_carrera);
	}

	protected function buildQuery()
	{
		$query = parent::buildQuery();

		$alias = $query->getRootAlias();
		$query->leftJoin($alias . '.Carrera c');
		$query->andWhere( 'c.slug = ?', $this->getRequest()->getUrlParameter('carrera_slug'));
		return $query;
	}
	
}
